package br.com.usjt.sge.reserva;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import br.com.usjt.sge.complemento.EnderecoRN;
import br.com.usjt.sge.estacionamento.Estacionamento;
import br.com.usjt.sge.estacionamento.EstacionamentoRN;

@ManagedBean
public class MapBean implements Serializable {
	
	private MapModel advancedModel;
	private Marker marker;
	private String endereco;

	public MapBean() {
		advancedModel = new DefaultMapModel();
		
		//Shared coordinates
		LatLng coord1 = new LatLng(-23.56912,-46.713227);  
        LatLng coord2 = new LatLng(-23.571933,-46.704816);  
        LatLng coord3 = new LatLng(-23.568747,-46.714901);  
        LatLng coord4 = new LatLng(-23.570635,-46.709086);  
		
		//Icons and Data
		advancedModel.addOverlay(new Marker(coord1, "Konyaalti", "konyaalti.png", "http://maps.google.com/mapfiles/ms/micons/blue-dot.png"));
		advancedModel.addOverlay(new Marker(coord2, "Ataturk Parki", "ataturkparki.png"));
		advancedModel.addOverlay(new Marker(coord4, "Kaleici", "kaleici.png", "http://maps.google.com/mapfiles/ms/micons/pink-dot.png"));
		advancedModel.addOverlay(new Marker(coord3, "Karaalioglu Parki", "karaalioglu.png", "http://maps.google.com/mapfiles/ms/micons/yellow-dot.png"));
	}
	
	public void pesquisar() {
		
		advancedModel = new DefaultMapModel();
						
		Integer codigoPesquisado;
		List<Estacionamento> listaDeEstacionamentos;
								
		EnderecoRN enderecoRN = new EnderecoRN();
		EstacionamentoRN estacionamentoRN = new EstacionamentoRN();
		
		codigoPesquisado = enderecoRN.buscarPorLogradouro(this.endereco);
				
		listaDeEstacionamentos = estacionamentoRN.pesquisarPorCodigoEndereco(codigoPesquisado);
				
		
		for (Estacionamento estacionamento : listaDeEstacionamentos) {
									
			LatLng coord = new LatLng(Double.parseDouble(estacionamento.getLat()), Double.parseDouble(estacionamento.getLng()));
			
			advancedModel.addOverlay(new Marker(coord, "Konyaalti", "konyaalti.png", "http://maps.google.com/mapfiles/ms/micons/blue-dot.png"));
					
			
		}
				
	}

	public MapModel getAdvancedModel() {
		return advancedModel;
	}
	
	public void onMarkerSelect(OverlaySelectEvent event) {
		marker = (Marker) event.getOverlay();
	}
	
	public Marker getMarker() {
		return marker;
	}
	public String getEndereco() {
		return endereco;
	}
	public void setEndereco(String endereco) {
		this.endereco = endereco;
	}
}